package edu.infomatik.tundra.pages;

import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.util.EnumSelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.security.SystemAccessBean;
import edu.infomatik.tundra.logic.security.UserTypes;

@SuppressWarnings("unused")
public class Registration {
	
	@Inject
	private Messages messages;
	@Property
	private String userName="";
	@Property
	private String userPwd;
	@Property
	private String userPwd2;
	@Property
	private String userEmail;
	@Property
	private UserTypes userType;
	@Component
	private Form regForm;
	@Component(id="userPwd")
	private PasswordField password;

	private SystemAccessBean user ;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	public SelectModel getUserTypes() {
		return new EnumSelectModel(UserTypes.class, messages);
	}
	void onValidateForm(){
		
		if (null!=userName && userName.length()>=3){
			if (!SystemAccessBean.isUNameUnique(userName))
				regForm.recordError(password, messages.get("reg-userName-not-unique"));
		}else regForm.recordError(password, messages.get("reg-userName-not-unique"));					

		try {
			if ( !userPwd2.equals(userPwd)&& null!=regForm && null!=password )
				regForm.recordError(password, messages.get("reg-passwords-not-equal"));
		} catch (NullPointerException e) {
			e.printStackTrace();
			e=null;
		}
	}
}
